package com.zhf.controller;


import com.zhf.pojo.EsUser;
import com.zhf.service.UserService;
import com.zhf.util.R;
import com.zhf.utils.OSSFileUtil;
import com.zhf.vo.UserVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author 郑海飞
 * @version 1.0
 * @description:
 * @date 2024/3/27 9:32
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    OSSFileUtil ossFileUtil;

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    /**
     * @Author 郑海飞
     * @Description //用户上传头像方法
     * @Date 9:37 2024/3/27
     * @Param
     * @param file
     * @return com.zhf.util.R
     */
    @PostMapping("upload")
    public R upload(MultipartFile file) throws IOException {
        //文件上传
        String url = ossFileUtil.uploadFile(file);
        //返回结果
        return R.ok().message("图片上传成功").data("url","https://"+url);
    }

    /**
     * @Author 郑海飞
     * @Description //发送短信方法
     * @Date 9:39 2024/3/27
     * @Param
     * @param userVo
     * @return com.zhf.util.R
     */
    @PostMapping("sendCode")
    public R sendCode(@RequestBody UserVo userVo){
        //发送短信
        userService.sendCode(userVo);
        return R.ok().message("短信发送成功");
    }

    /**
     * @Author 郑海飞
     * @Description //用户注册方法
     * @Date 10:14 2024/3/27
     * @Param
     * @param userVo
     * @return com.zhf.util.R
     */
    @PostMapping("save")
    public R save(@RequestBody UserVo userVo){
        //调用service
        userService.save(userVo);
        //返回结果
        return R.ok().message("用户注册成功");
    }
    /**
     * @Author 郑海飞
     * @Description //创建索引功能
     * @Date 10:32 2024/3/27
     * @Param
     * @return com.zhf.util.R
     */
    @PostMapping("create")
    public R create(){
        //创建索引
        IndexOperations indexOps = elasticsearchRestTemplate.indexOps(EsUser.class);
        //创建索引
        indexOps.create();
        //创建映射
        Document mapping = indexOps.createMapping();
        //设置映射
        indexOps.putMapping(mapping);
        //返回结果
        return R.ok().message("索引创建成功");
    }
    /**
     * @Author 郑海飞
     * @Description //用户列表页面
     * @Date 10:40 2024/3/27
     * @Param
     * @param userVo
     * @return com.zhf.util.R
     */
    @PostMapping("list")
    public R list(@RequestBody UserVo userVo){
        return userService.userList(userVo);
    }

    /**
     * @Author 郑海飞
     * @Description //修改用户会员等级
     * @Date 11:31 2024/3/27
     * @Param
     * @param id
     * @return com.zhf.util.R
     */
    @PostMapping("update/{id}")
    public R update(@PathVariable("id")Integer id){
        //返回结果
        return userService.update(id);
    }


}
